<template>
  <div class="AddOrUpdate_main add_container tenantMenu_main main_container user_container">
    <div v-loading="loading" style="margin-right:0;padding-top:0" class="main_main">
      <div class="main_main_container">
        <div class="main_main_left">
          <div class="title">检验模板</div>
          <div class="main_main_left_top">
            <div class="leftClass">
              <div v-for="item in searchData" :key="item.id" :class="[item.id==rowId?'activeClass':'']" class="rowClass" @click.stop="getTemplateInfo(item)">
                <div v-if="item.rowClassTwo == false">{{ item.name }}</div>
                <div v-else>
                  <el-tooltip :content="item.name" placement="top">
                    <div>{{ item.name.substring(0,10)+'...' }}</div>
                  </el-tooltip>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="main_main_right">
          <div style="padding-right:20px" class="SearchBar_main main_main_search">
            <div style="padding-left:0" class="main_main_search_item">
              <div class="formItem_header">
                <div class="formItem_header_operate">
                  <a class="formItem_header_operate_items" @click="add">
                    <svg-icon icon-class="新增" />
                    <div class="item_name">新增</div>
                  </a>
                </div>
              </div>
            </div>
            <div class="main_main_search_item">
              <el-input v-model="pageData.searchValue" style="width:200px;margin-right:10px" placeholder="请输入检验项目" clearable @keyup.enter.native="getLists" @clear="getTemplateInfoOne(itemRow)" />
              <el-button style="background:#387bfc;width:30px;" icon="el-icon-search" @click.stop="getLists" />
            </div>
          </div>
          <div ref="table_container" style="margin-top:20px" class="table_container">
            <el-table
              ref="table"
              :data="tableData"
              stripe
              border
              :height="340"
              style="width: 99.9%"
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
              @sort-change="
                (column) => {
                  sortChange(column, this);
                }
              "
              @selection-change="
                (val) => {
                  selectionChange(val, this);
                }
              "
            >
              <el-table-column type="selection" width="70" align="center" />
              <el-table-column prop="sort" label="序号" width="80">
                <template slot-scope="scope">
                  <div v-if="!scope.row.isAdd">{{ scope.row.rankId }}</div>
                  <div v-else />
                </template>
              </el-table-column>
              <el-table-column prop="qualityItemName" label="检验项目" :show-overflow-tooltip="true" />
              <el-table-column prop="itemPropertyName" label="项目属性" :show-overflow-tooltip="true" />
              <el-table-column prop="itemDimensionName" label="项目维度" :show-overflow-tooltip="true" />
              <el-table-column prop="qualityCheckType" label="质检方式" :show-overflow-tooltip="true" />
              <el-table-column prop="qualityCheckTool" label="检验工具" :show-overflow-tooltip="true" />
              <el-table-column prop="qualityCheckStandard" label="检验标准" :show-overflow-tooltip="true" />
            </el-table>
          </div>

          <!--          <div style="margin:0.625rem .625rem 0">-->
          <!--            <pagination-->
          <!--              :total="total"-->
          <!--              :page.sync="pageData.pageNum"-->
          <!--              :limit.sync="pageData.pageSize"-->
          <!--              :selected-num="selectionsAll.length"-->
          <!--              @pagination="getList"-->
          <!--            />-->
          <!--          </div>-->
        </div>
      </div>
    </div>
    <div style="background:white;border-top:.0625rem solid #ECECEC;" class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>
import {
  list,
  getInfo
} from '@/api/qualityManage/checkoutTemplate'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面

export default {
  name: 'Index',
  props: {
    layerid: {},
    defaultValue: {},
    selectedRow: {}
  },
  data() {
    return {
      loading: true,
      pageId: '检验模板', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        searchValue: ''
      },
      templateItem: '',
      rowId: '',
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      searchData: [],
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      selectionsAllOne: [],
      templateName: '',
      templateId: '',
      remark: '',
      itemRow: {},
      allData: []
    }
  },
  mounted() {
    this.getList()
    this.$nextTick(() => {
      this.tableHeight = this.$refs.table_container.offsetHeight
    })
    if (this.defaultValue.id) {
      this.getTemplateInfo(this.defaultValue)
    }
    this.selectionsAll = this.selectedRow
  },
  methods: {
    getLists() {
      this.selectionsAllOne = JSON.parse(JSON.stringify(this.selectionsAll))
      this.tableData = this.allData.filter(item => item.qualityItemName.indexOf(this.pageData.searchValue) !== -1)
      this.$nextTick(() => {
        this.selectionsAll = this.selectionsAllOne
        this.setSelectRow(this)// 设置行选中
      })
    },
    getList() {
      list({ ...this.pageData, status: '1' }).then((res) => {
        this.searchData = res.data
        this.searchData.forEach(item => {
          if (item.name.length > 10) {
            item.rowClassTwo = true
          } else {
            item.rowClassTwo = false
          }
        })
        this.total = res.total
        this.loading = false
      })
    },
    getTemplateInfoOne() {
      getInfo(this.rowId).then(res => {
        this.tableData = res.data.data
        this.$nextTick(() => {
          this.selectionsAll = this.selectionsAllOne
          this.setSelectRow(this)// 设置行选中
        })
      })
    },
    // 查询详情
    getTemplateInfo(item) {
      console.log(item)
      this.itemRow = item
      this.rowId = item.id
      this.templateName = item.name
      this.templateId = item.id
      this.templateItem = item
      this.selections = []
      this.selectionsAll = []
      getInfo(item.id).then(res => {
        this.tableData = res.data.data
        this.allData = JSON.parse(JSON.stringify(res.data.data))
        this.remark = res.data.remark
        this.$nextTick(() => {
          this.tableData.forEach(ele => {
            if (this.defaultValue.id == item.id) {
              if (this.selectedRow.some(item3 => item3.qualityTemplateDetailId == ele.id)) { this.$refs.table.toggleRowSelection(ele, true) }
            } else {
              this.$refs.table.toggleRowSelection(ele, true)
            }
          })
        })
      })
    },
    add() {
      this.$layer.iframe({
        // 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {} // props
        },
        area: ['1320px', '700px'],
        title: '新增检验模板', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    cancel() {
      this.$layer.close(this.layerid) // 关闭弹框
    },
    onSubmit() {
      this.$parent.showTemplate(this.selections, this.templateName, this.templateId, this.remark, this.templateItem)
      this.$layer.close(this.layerid) // 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-tooltip {
  padding-left: 0px !important;
}
.activeClass{
  background-color: rgba(56, 123, 252, 1);
  color: white;
}
.leftClass {
  flex: 2;
  // margin-right: 45px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-left: 10px;
  // &:nth-child(even) {
  //   background: #148519;
  // }
  // &:nth-child(odd) {
  //   background: #eef0f4;
  // }
  .rowClass:hover{
    background-color: rgba(56, 123, 252, 0.5);
    cursor: pointer;
    color: white;
  }
  div {
    height: 40px;
    width: 100%;
    padding-left:10px ;
    // text-align: center;
    line-height: 40px;
  }
}
.main_main {
  height: calc(100% - 50px);
}
.main_main_container {
  width: 100%;
  height: 100%;
  display: flex;

  .main_main_left {
    width: 250px;
    height: calc(100% - 20px);
    padding-right: 20px;
    .title {
      padding: 10px 0;
      border-bottom: 1px solid #387BFC;
      margin-bottom: 10px;
    }
    .main_main_left_top {
      width: 100%;
      height: calc(100% - 80px);
      overflow-y: auto;
      .el-tree {
        background-color: transparent;
      }
      /deep/ .el-tree-node__content:hover {
        background-color: rgba(56, 123, 252, 0.5) !important;
        color: white;
      }
    }
    .main_main_left_bottom {
      width: 100%;
      .main_main_left_bottom_item {
        background-color: rgba(74, 77, 82, 0.2);
        height: 40px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding-left: 24px;
      }
      .main_main_left_bottom_item:last-child {
        border-top: 1px solid white;
      }
      .main_main_left_bottom_item:hover {
        background-color: rgba(56, 123, 252, 0.5);
        color: white;
      }
      .active {
        .main_main_left_bottom_item {
          background-color: rgba(56, 123, 252, 1);
          color: white;
        }
      }
    }
  }
  .main_main_right {
    width: calc(100% - 250px);
    height: 100%;
    display: flex;
    flex-direction: column;
    border-left: 1px solid #ECECEC;

  }
}
.button{
  width: 100px;
  height: 30px;
  color: #fff;
  background-color: #387bfc;
  border-radius: 4px;
  text-align: center;
  line-height: 30px;
}
.formItem_header{
  justify-content: flex-start !important;
  align-items: flex-start !important;
  height: 40px !important;
}
.formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}
.spotClass {
  /deep/.el-button {
    span {
      overflow: hidden; //超出的文本隐藏
      text-overflow: ellipsis; //溢出用省略号显示
      white-space: nowrap; //溢出不换行
      width: 150px;
    }
  }
}
</style>
